/*
    #include <signal.h>
    int sigaction(int signumber, const struct sigaction * act, struct sigaction *oldact);
    
    // struct sigaction
    struct sigaction {
      void (*sa_handler)(int);
      void (*sa_sigaction)(int, siginfo_t *, void *);
      sigset_t sa_mask;
      int sa_flags;
    };

    // siginfo_t
    typedef struct siginfo {
        int si_signo; // Signal number
        int si_errno; // An errno value
        int si_code; // Signal code
        pid_t si_pid; // Sending process ID
        uid_t si_uid; // Real user ID of  Sending process
        int si_status; // Exit value or signal
        clock_t si_utime; // User Time consumed
        clock_t si_stime; // System time consumed
        sigval_t si_balue; // Signal value
        int si_int; // POSIX.1b signal
        void *si_ptr; // POSIX.1b signal
        void *si_addr; // Memory location that caused fault
        int si_band; // Band event
        int si_fd; // File descriptor
    } siginfo_t;
*/

#include <stdio.h>
#include <signal.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
/*
    SIGTERM : when kill(1) signal
    调用kill(pid_t)命令时缺省产生的信号
*/

void terminatehandler(int signumber);

int main(int argc, char * argv[]) {
    char buffer1[100], buffer2[100];
    int i;
    struct sigaction act;
    act.sa_handler = terminatehandler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;

    if(sigaction(SIGTERM, &act, NULL) == -1) {
        fprintf(stdout, "%s\n", "Couldnot register signal signal handler!");
    }

    fprintf(stdout, "Pid of This Process: %6d.\n", getpid());
    for( ; ; ) {
        fgets(buffer1, sizeof(buffer1), stdin);
        for(i=0; i<100; i++) {
            buffer2[i] = buffer1[i] >= 'a' ? buffer1[i] - 32 : buffer1[i];
        }
        fprintf(stdout, "You input is : %s\n\n", buffer2);
    }

    return 0;
}

void terminatehandler(int signumber) {
    fprintf(stdout, "%s\n", "Signal SIGTERM.");
    exit(-1);
}
